package de.jmda.gen.java.impl;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;

import de.jmda.gen.GeneratorException;
import de.jmda.gen.LineIndenter;
import de.jmda.gen.java.ClassGenerator;
import de.jmda.gen.java.DeclaredFieldGenerator;
import de.jmda.gen.java.InstanceFieldGenerator;
import de.jmda.gen.java.naming.ImportManager;
import de.jmda.gen.java.naming.ImportManagerProvider;

public class JUTManagedImportStatementsGenerator
{
	private final static Logger LOGGER =
      Logger.getLogger(JUTManagedImportStatementsGenerator.class);

	@Before
	public void before()
	{
		ImportManagerProvider.newImportManager("some.package", "SomeType");
	}
	
	@Test
	public void testManagedImportStatementsGenerator()
			throws GeneratorException
	{
		ClassGenerator generator = new DefaultClassGenerator();

		ImportManager importManager = ImportManagerProvider.getImportManager();

		generator.setPackageName(getClass().getPackage().getName());
		generator.setImportStatementsGenerator(
				new ManagedImportStatementsGenerator(importManager));
		generator.setImplementsClause(importManager.useType(Serializable.class));
		generator.addFieldGenerator(getInstanceDateFieldGenerator1());
		generator.addFieldGenerator(getInstanceDateFieldGenerator2());

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated [" + generated + "]");
	}

	private InstanceFieldGenerator getInstanceDateFieldGenerator1()
  {
		InstanceFieldGenerator result =
				new DefaultInstanceFieldGenerator();

		DeclaredFieldGenerator generator =
				new DefaultDeclaredInstanceFieldGenerator();

		generator.setTypeNameGenerator(
				new DefaultTypeNameGenerator(
						ImportManagerProvider
								.newImportManager()
								.useType(java.util.Date.class)));

		result.setLineIndenter(LineIndenter.TAB_SINGLE);
		result.setDeclaredFieldGenerator(generator);
		
	  return result;
  }

	private InstanceFieldGenerator getInstanceDateFieldGenerator2()
  {
		InstanceFieldGenerator result =
				new DefaultInstanceFieldGenerator();
		
		DeclaredFieldGenerator generator =
				new DefaultDeclaredInstanceFieldGenerator();

		generator.setTypeNameGenerator(
				new DefaultTypeNameGenerator(
						ImportManagerProvider
								.newImportManager()
								.useType(java.sql.Date.class)));

		result.setLineIndenter(LineIndenter.TAB_SINGLE);
		result.setDeclaredFieldGenerator(generator);
		
	  return result;
  }
}